package com.example.beltequipmentdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.beltequipmentdemo.View.WaveShowView;
import com.example.beltsdk.TheBelt;
import com.vmove.signalproc.SignalProc;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import cn.com.heaton.blelibrary.ble.utils.ByteUtils;

public class MainActivity extends AppCompatActivity {
    private WaveShowView waveview;
    private static Context mContext;
    private RefreshListAdapter mAdapter;
    private ListView data_fission;
    private List<RefreshListItem> data1 = new LinkedList<>();
    private RefreshListItem item;
    private TextView state, disconnect, data_a;
    private TextView heart_rate;//心率
    private TextView humidity,shuju;//呼吸
    private TextView actual_temperature_a, actual_temperature_b, actual_temperature_c;
    private int value1, value2, value3, value4, value5, value6, value7, value8, value9;
    private TextView include_top_bar_tv_more;
    private String m;
    private     SignalProc mSignalProc=new SignalProc();
    private Handler bleHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                //搜索并连接
                case 1:
                    DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                    byte[] data = (byte[]) msg.obj;
                    byte[] useful_data = new byte[15];
                    int[] ints = new int[5];
                    System.arraycopy(data, 1, useful_data, 0, 15);
                    int a = 0;
                    int ecg = 0;
                    for (int i = 0; i < useful_data.length; i = i + 3) {
                        ecg = ((data[i] & 0xFF) << 16) | ((data[i + 1] & 0xFF) << 8) | ((data[i + 2] & 0xFF));
                        int b1 = useful_data[i]; //s
                        short num = useful_data[i + 1]; //u
                        int b2 = num & 0xff;
                        short num1 = useful_data[i + 2];
                        int b3 = num1 & 0xff; //u
                        int b4 = b1 * 65536 + b2 * 256 + b3;
                        ints[a] = b4;
                        float b5=(b4+8200000)/5000;
                        a++;
//                        waveview.showLine(b5);//取得是-20到20间的浮点数
//                        waveview.postInvalidate();
                        int b6=   mSignalProc.Run(b4);


                        waveview.showLine(b6);//取得是-20到20间的浮点数
                        waveview.postInvalidate();
                        Log.d("接收数据转化",""+b4+"转化:"+b6);
                    }

                    Addthedata(Arrays.toString(ints));
                    if (data[0] == -24) {
                        Log.d("aqqa", ByteUtils.toHexString(data));

                        switch (data[16]) {
                            //心率
                            case -96:
                                value4 = getShort(data[18], data[17]);

                                heart_rate.setText(value4 + "");

                                break;
                            //呼吸
                            case -80:
                                value5 = getShort(data[18], data[17]);

                                humidity.setText(value5 + "");

                                break;
                            //A
                            case -64:
                                value6 = getShort(data[18], data[17]);

                                actual_temperature_a.setText(df.format((float) value6 / 10));

                                break;
                            //b
                            case -63:
                                value7 = getShort(data[18], data[17]);

                                actual_temperature_b.setText(df.format((float) value7 / 10));

                                break;
                            //c
                            case -62:
                                value8 = getShort(data[18], data[17]);

                                actual_temperature_c.setText(df.format((float) value8 / 10));

                                break;
                        }

                    }

                    break;
                case 2:
                    String data1  = (String) msg.obj;

                    Addthedata(data1);

                    break;

            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSignalProc.Init(250);
        waveview=findViewById(R.id.waveview);
        state = findViewById(R.id.state);
        disconnect = findViewById(R.id.disconnect);
        heart_rate = findViewById(R.id.heart_rate);
        data_fission = findViewById(R.id.data_fission);
        humidity = findViewById(R.id.humidity);
        actual_temperature_a = findViewById(R.id.actual_temperature_a);
        actual_temperature_b = findViewById(R.id.actual_temperature_b);
        actual_temperature_c = findViewById(R.id.actual_temperature_c);
        include_top_bar_tv_more = findViewById(R.id.include_top_bar_tv_more);
        include_top_bar_tv_more.setText("清空数据");
        include_top_bar_tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data1.clear();
                item = new RefreshListItem();
                item.ma = "已清空";
                data1.add(item);
                mAdapter.setData(data1, 1 == 1 ? true : false);

                data_fission.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                data1.clear();
            }
        });
        mAdapter = new RefreshListAdapter(this);
        data_fission.setAdapter(mAdapter);
        state = findViewById(R.id.state);
        //初始化
        TheBelt.sharedCenter().Initializethe(MainActivity.this, mContext);
        //连接
        state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TheBelt.sharedCenter().Macconnection("9C:A5:25:99:1E:ff");
            }
        });
        //连接状态
        TheBelt.sharedCenter().setReceivedCallback(new TheBelt.OnConnectionstatus() {
            @Override
            public void state(boolean mTheconnected, boolean mIntheconnection, boolean mNotconnected) {
                if (mTheconnected) {
                    state.setText("已连接");
                    disconnect.setText("断开连接");
                } else if (mIntheconnection) {
                    state.setText("连接中...");
                } else if (mNotconnected) {
                    state.setText("未连接");
                    disconnect.setText("已断开连接");
                }
            }
        });

        //数据微气候监测仪
        TheBelt.sharedCenter().setReceivedata(new TheBelt.OnReceivedata() {
            @Override
            public void data(byte[] data) {
                Log.d("蓝牙收到数据", ByteUtils.toHexString(data));
                Message message = new Message();
                message.what = 1;
                message.obj = data;
                bleHandler.sendMessage(message);


            }
        });
        disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TheBelt.sharedCenter().Disconnect();

            }
        });

        TheBelt.sharedCenter().setscandata(new TheBelt.Onscanning() {
            @Override
            public void number(String name, String Address) {
                String S = "蓝牙名字" + name + "地址" + Address;
                Message message1 = new Message();
                message1.what = 2;
                message1.obj = Address;
                bleHandler.sendMessage(message1);


            }
        });


    }

    public static short getShort(byte argB1, byte argB2) {
        return (short) ((argB1 & 0xFF) | (argB2 << 8));
    }

    private void Addthedata(String data) {

        item = new RefreshListItem();
        item.ma = data;
        data1.add(item);
        mAdapter.setData(data1, 1 == 1 ? true : false);
        data_fission.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        data1.clear();

    }

    private static class RefreshListItem {
        public String ma;
    }

    //单条布局
    private static class RefreshListAdapter extends BaseAdapter<RefreshListItem> {

        public RefreshListAdapter(Activity context) {
            super(context);
        }

        @Override
        protected int getItemLayoutId(int itemViewType) {
            return R.layout.data_lits;
        }

        @Override
        protected void handleItem(int itemViewType, int position, final RefreshListItem item, ViewHolder holder, boolean reused) {
            holder.get(R.id.monotonous_data, TextView.class).setText(item.ma);


        }
    }
    public static float getV(int data) {
        float a = (float) (data - 8388608) * 4800f;
        float y = (float) (a / 16777215 / 6f);
        Log.d("计算公式",""+y);
        return y;
    }
}